package com.leesche.zcsp.node.utils.web;

import com.leesche.zcsp.node.utils.HttpRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author Jerry Chan
 * @version V1.0
 * @Title: node
 * @Package com.leesche.zcsp.node.utils.web.gernerator
 * @Description:( 生成当前通信的请求与会话 )
 * @date 2016年10月14日 22:31
 */
public class SessionManager {

    /**
     * @param key 属性值的键值
     * @return 当前会话中的属性
     * @Title: getSessionValue(String key)
     * @Description: TODO(获取当前会话中的属性)
     * @author Jerry
     * @date 2016-10-14 11:15:34
     */
    public static Object getSessionAttribute(String key) {
        return getSession().getAttribute(key);
    }

    /**
     * @param key    属性值的键值
     * @param object 会话中的属性
     * @Title: setSessionAttribute(String key, Object object)
     * @Description: TODO(设置当前会话的属性)
     * @author Jerry
     * @date 2016-10-15 12:01:52
     */
    public static void setSessionAttribute(String key, Object object) {
        getSession().setAttribute(key, object);
    }

    /**
     * @Title: destroySession()
     * @Description: TODO(销毁当前会话)
     * @author Jerry
     * @date 2016-10-17 03:28:00
     */
    public static void destroySession() {
        getSession().invalidate();
    }

    /**
     * @param maxInactiveInterval Seeion的最大有效期（ 整数：单位[分钟] ）
     * @Title: setSeesionInvalidDate(int maxInactiveInterval)
     * @Description: TODO(设置当前Seeion的最大有效期)
     * @author Jerry
     * @date 2016-10-18 06:57:18
     */
    public static void setSeesionInvalidDate(int maxInactiveInterval) {
        getSession().setMaxInactiveInterval(maxInactiveInterval * 60);
    }

    /**
     * @return ServletContext web工程的上下文
     * @Title: getContext
     * @Description: TODO(获取web工程的上下文)
     * @author Jerry Chen
     * @date 2016年10月22日 上午2:55:11
     */
    public static ServletContext getContext() {
        return getSession().getServletContext();
    }

    public static String getWebServerURL() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            //return request.getScheme() + "://" + request.getServerName() +  ":"  +  request.getServerPort() + "/";
            return request.getScheme() + "://" + request.getServerName() + "/";

        }

        return null;
    }


    private static HttpSession getSession() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            return ((ServletRequestAttributes) requestAttributes).getRequest().getSession();
        }
        return null;
    }

}
